001 /* 002 * Copyright 2003-2005 The Apache Software Foundation 003 * Copyright 2005 Stephen McConnell 004 * 005 * Licensed under the Apache License, Version 2.0 (the "License"); 006 * you may not use this file except in compliance with the License. 007 * You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 package net.dpml.cli; 018 019 import java.util.Collections; 020 import java.util.Set; 021 022 import net.dpml.cli.resource.ResourceHelper; 023 024 /** 025 * A problem found while dealing with command line options. 026 * 027 * @author <a href="http://www.dpml.net">Digital Product Meta Library</a> 028 * @version 1.0.0 029 */ 030 public class OptionException extends Exception 031 { 032 /** 033 * The settings used when displaying the related Option. 034 * 035 * @see DisplaySetting 036 */ 037 public static final Set HELP_SETTINGS = 038 Collections.unmodifiableSet( 039 Collections.singleton( DisplaySetting.DISPLAY_PROPERTY_OPTION ) ); 040 041 /** resource HELPER instance */ 042 private static final ResourceHelper HELPER = ResourceHelper.getResourceHelper(); 043 044 /** The Option the exception relates to */ 045 private final Option m_option; 046 047 /** The message explaining the Exception */ 048 private final String m_message; 049 050 /** 051 * Creates a new OptionException. 052 * 053 * @param option the Option the exception relates to 054 */ 055 public OptionException( final Option option ) 056 { 057 this( option, null, null ); 058 } 059 060 /** 061 * Creates a new OptionException. 062 * @param option the Option the exception relates to 063 * @param messageKey the id of the message to display 064 */ 065 public OptionException( 066 final Option option, final String messageKey ) 067 { 068 this( option, messageKey, null ); 069 } 070 071 /** 072 * Creates a new OptionException. 073 * @param option the Option the exception relates to 074 * @param messageKey the id of the message to display 075 * @param value a value to display with the message 076 */ 077 public OptionException( 078 final Option option, final String messageKey, final String value ) 079 { 080 m_option = option; 081 if( messageKey != null ) 082 { 083 final StringBuffer buffer = new StringBuffer(); 084 if( value != null ) 085 { 086 buffer.append( HELPER.getMessage( messageKey, value ) ); 087 } 088 else 089 { 090 buffer.append( HELPER.getMessage( messageKey ) ); 091 } 092 buffer.append( " " ); 093 m_option.appendUsage( buffer, HELP_SETTINGS, null ); 094 m_message = buffer.toString(); 095 } 096 else 097 { 098 m_message = ""; 099 } 100 } 101 102 /** 103 * Gets the Option the exception relates to 104 * 105 * @return The related Option 106 */ 107 public Option getOption() 108 { 109 return m_option; 110 } 111 112 /** 113 * Return the exception message. 114 * @return the exception message 115 */ 116 public String getMessage() 117 { 118 return m_message; 119 } 120 }